1. Field of the Invention
The present invention relates to a data processing apparatus and method for saving return state, and more specifically to the saving of the return state in a data processing apparatus incorporating a processing unit having a plurality of modes of operation, with each mode of operation having a corresponding stack storing data associated with that mode.
2. Description of the Prior Art
Within such a data processing apparatus, it is known to provide instructions which are arranged to save return state data on the stack associated with a current mode of operation. A typical example of where such instructions are used is upon occurrence of an exception, in which event the return state data is saved on the stack associated with the exception mode of operation. This procedure may be required for a variety of reasons. For example, such a data processing apparatus may typically support multi-tasking, and when a context switch is to be performed in order to enable another task to execute, the stack or stacks associated with the modes of operation of the current task need to be swapped in order to enable that new task to execute. In systems where the stacks are accessed using stack pointers, this swapping is usually performed by swapping the stack pointer or stack pointers of the various stacks. In such a data processing apparatus, it is typically required that the return state data is saved on a stack which will be swapped when the context switch occurs.
In order to facilitate context switching, it is required that all stacks that are non-empty at the time of the context switch are swapped. On a processor with multiple stacks, this can require swapping every stack belonging to a particular task. Additionally, if this approach is used, a separate stack per mode of operation has to be maintained for each task, and this leads to increased memory consumption and increased time taken to swap each stack belonging to each task. For example, the current processor produced by ARM Limited has six separate stacks corresponding to six modes of operation, namely System/User, FIQ (Fast Interrupt Request), Supervisor, Abort, IRQ (Interrupt ReQuest) and Undefined, where FIQ and IRQ are two interrupt modes of operation. Therefore, using the above approach, a context switch may require the swapping of six separate stacks, and each task may have to reserve memory for six separate stacks. An example of such an approach will be discussed later with reference to FIGS. 2A and 2B. FIGS. 2A and 2B show a system with five rather than six stacks, since they illustrate an example of a typical system where it is assumed that the FIQ mode is reserved for a single high priority interrupt which will not cause a context switch.
An alternative, more commonly used, approach is to share the various exception stacks amongst the tasks, and to then transfer any data on an exception stack to either the user stack or a single per process privileged mode stack on entry to an exception. Taking the earlier example of the processor currently produced by ARM Limited, this would equate to transferring data from the FIQ, Abort, IRQ or Undefined stacks to the User or Supervisor stacks. This approach ensures that all exception stacks (FIQ, Abort, IRQ and Undefined in the example of the ARM processor) remain empty at all points where a context switch may be required.
The data saved on entry to an exception is usually that data which is required for the task to continue execution from the point at which the exception occurred. This data is termed the “return state data”.
The above-described second approach requires that on entry to an exception, the return state is saved on the stack associated with the exception mode of operation, the processor user or privileged mode is entered, and the return state data is then read back from the stack associated with the exception mode of operation and saved on the stack associated with the user or privileged mode. The return state cannot simply be saved from the user or privileged mode after the user or privileged mode has been entered, because the return state is not accessible from that mode. This process will be discussed in more detail later with reference to the example of FIGS. 3A and 3B.
This process of saving the return state data on the stack belonging to the exception mode of operation, changing mode, recovering the return state data from the stack belonging to the exception mode of operation, and saving the return state data on the user or privileged mode stack, requires a significant number of instructions which adversely affects code size and performance of the data processing apparatus.
Additionally, it should be noted that this processing is required at a critical point in interrupt handling when interrupts are disabled, therefore leading to significant interrupt latency. It should be noted that interrupts cannot be re-enabled until the return state data is saved on the user or privileged mode stack in case a further interrupt arrives and overwrites the return state data before it an be saved.
Accordingly, it is desirable to provide a more efficient technique for saving the return state data upon occurrence of an exception.